package com.gm.wholesale.module.good.model;

import android.util.ArrayMap;

import com.gm.commonlib.base.baseBean.BaseListResp;
import com.gm.commonlib.base.baseBean.BaseResp;
import com.gm.commonlib.base.baseBean.DataResp;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.network.HttpClient;
import com.gm.commonlib.network.HttpServicesHolder;
import com.gm.wholesale.module.good.api.IGoodApi;
import com.gm.wholesale.module.good.bean.CreateGoodReq;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.good.bean.GoodDetailBean;
import com.gm.wholesale.module.kind.bean.KindBean;

import io.reactivex.Observable;

public class GoodModel {

    public static void getCategory(BaseFragment activity, int type, BaseHttpObserver<BaseListResp<KindBean>> observer) {
        HttpClient.sendRequest(activity, new HttpServicesHolder<IGoodApi, BaseListResp<KindBean>>() {
            @Override
            public Observable<BaseListResp<KindBean>> getHttpObservable(IGoodApi instance) {
                ArrayMap<String, String> queryMap = new ArrayMap<>();
                //是否基于sku筛选，0表示展示全部分类，1表示展示仅有商品的分类
                queryMap.put("filterSku", type+ "");
                return instance.getCategory(queryMap);
            }
        }, observer);
    }


    //获取临时商品
    public static void getTemporary(BaseFragment fragment, BaseHttpObserver<DataResp<GoodBean>> observer) {
        HttpClient.sendRequest(fragment, new HttpServicesHolder<IGoodApi, DataResp<GoodBean>>() {
            @Override
            public Observable<DataResp<GoodBean>> getHttpObservable(IGoodApi instance) {
                return instance.getTemporary();
            }
        }, observer);
    }

    //语音输入查询商品
    public static void getSearchGood(IBaseView iBaseView, ArrayMap<String,Object> queryData, BaseHttpObserver<BaseListResp<GoodBean>> observer) {
        HttpClient.sendRequest(iBaseView, new HttpServicesHolder<IGoodApi, BaseListResp<GoodBean>>() {
            @Override
            public Observable<BaseListResp<GoodBean>> getHttpObservable(IGoodApi instance) {
                return instance.getGoodByName(queryData);
            }
        }, observer);
    }



    //新增商品
    public static void createGood(IBaseView iBaseView, CreateGoodReq data, BaseHttpObserver<BaseResp> observer) {
        HttpClient.sendRequest(iBaseView, new HttpServicesHolder<IGoodApi, BaseResp>() {
            @Override
            public Observable<BaseResp> getHttpObservable(IGoodApi instance) {
                return instance.createGood(data);
            }
        }, observer);
    }


    //删除商品
    public static void deleteGood(IBaseView iBaseView,long goodId, int version, BaseHttpObserver<BaseResp> observer) {
        HttpClient.sendRequest(iBaseView, new HttpServicesHolder<IGoodApi, BaseResp>() {
            @Override
            public Observable<BaseResp> getHttpObservable(IGoodApi instance) {
                ArrayMap<String,Object> data=new ArrayMap<>();
                data.put("version",version);
                return instance.deleteGood(goodId,data);
            }
        }, observer);
    }


    //商品商家or下架
    public static void updateGoodState(IBaseView iBaseView,long goodId, boolean isShelf, BaseHttpObserver<BaseResp> observer) {
        HttpClient.sendRequest(iBaseView, new HttpServicesHolder<IGoodApi, BaseResp>() {
            @Override
            public Observable<BaseResp> getHttpObservable(IGoodApi instance) {
                ArrayMap<String,Object> data=new ArrayMap<>();
                data.put("onShelf",isShelf);
                return instance.updateGoodState(goodId,data);
            }
        }, observer);
    }


    //获取商品详情
    public static void getGoodDetail(IBaseView iBaseView,long goodId, BaseHttpObserver<DataResp<GoodDetailBean>> observer) {
        HttpClient.sendRequest(iBaseView, new HttpServicesHolder<IGoodApi, DataResp<GoodDetailBean>>() {
            @Override
            public Observable<DataResp<GoodDetailBean>> getHttpObservable(IGoodApi instance) {

                return instance.getGoodDetail(goodId);
            }
        }, observer);
    }

   //修改商品
    public static void updateGood(IBaseView iBaseView,long goodId, CreateGoodReq data, BaseHttpObserver<BaseResp> observer) {
        HttpClient.sendRequest(iBaseView, new HttpServicesHolder<IGoodApi, BaseResp>() {
            @Override
            public Observable<BaseResp> getHttpObservable(IGoodApi instance) {
                return instance.updateGood(goodId,data);
            }
        }, observer);
    }



}
